//
//  KKNetworkButton.m
//  WritingBoard
//
//  Created by Mac mini on 16/1/23.
//  Copyright © 2016年 JuniorCodingPerson. All rights reserved.
//

#import "KKLoadingButton.h"

@interface KKLoadingButton() {
    
    CALayer *_kk_prgressLayer;
    CALayer *_kk_trackLayer;
}

@end

@implementation KKLoadingButton

- (void)setKk_progressTintColor:(UIColor *)kk_progressTintColor trackTintColor:(UIColor *)trackTintColor {
    
    if (kk_progressTintColor == nil || trackTintColor == nil) {
        return;
    }
    
    self.layer.masksToBounds = true;
    
    _kk_trackLayer = [CALayer layer];
    _kk_trackLayer.frame = [self bounds];
    _kk_trackLayer.backgroundColor = trackTintColor.CGColor;
    [self.layer addSublayer: _kk_trackLayer];
    
    _kk_prgressLayer = [CALayer layer];
    _kk_prgressLayer.frame = CGRectZero;
    _kk_prgressLayer.backgroundColor = kk_progressTintColor.CGColor;
    [self.layer addSublayer: _kk_prgressLayer];
}

- (void)setProgress:(double)progress animated:(BOOL)animated {
    
    if (progress > 1.0) {
        progress = 1.0;
    }
    
    CGFloat width = CGRectGetWidth(self.bounds);
    
    [UIView animateWithDuration: 0.25 animations:^{
        _kk_prgressLayer.frame = CGRectMake(0, 0, width * progress, CGRectGetHeight(self.bounds));
    }];
}

@end
